#include <bits/stdc++.h>
#define ll long long
using namespace std;

char buf[10000005];
int ppos, llen;

inline char gch()
{
    if (ppos == llen)
    {
        ppos = 0;
        llen = fread(buf, 1, 10000000, stdin);
    }
    if (ppos == llen) return -1;
    return buf[ppos++];
}

template < typename T >
inline void read(T &x)
{
    x = 0;
    bool flg = 0;
    char c;
    while ((c = gch()) < 48 || c > 57)
        if (c == '-') flg = 1;
    do
    {
        x = (x << 1) + (x << 3) + (c ^ 48);
    } while ((c = gch()) > 47 && c < 58);
    if (flg) x = -x;
}

const int N = 1e6 + 5;
int a[N];
ll f[N], mxn[N];

template < typename T >
inline void Chkmax(T &x, T y)
{
    if (y > x) x = y;
}

int main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    int T;
    read(T);
    while (T--)
    {
        int n;
        read(n);
        for (int i = 1; i <= n; ++i)
            read(a[i]);
        if (n == 1)
        {
            puts("0");
            continue;
        }
        f[0] = 0;
        for (int i = 1; i <= n; ++i)
        {
            f[i] = 0;
            mxn[a[i]] = LONG_LONG_MIN;
        }
        ll d = 0, abc = 0;
        for (int i = 2; i <= n; ++i)
        {
            int tmp = 0;
            if (a[i] == a[i - 1]) tmp = a[i];
            d += tmp;
            f[i - 1] = abc + d - tmp - d;
            if (mxn[a[i]] != LONG_LONG_MIN)
            {
                Chkmax(f[i - 1], mxn[a[i]] + d - tmp + a[i] - d);
            }
            // printf("%d %lld\n", i, f[i - 1]);
            Chkmax(mxn[a[i - 1]], f[i - 1]);
            Chkmax(abc, f[i - 1]);
        }
        ll ans = 0;
        for (int i = 1; i < n; ++i)
            Chkmax(ans, f[i] + d);
        printf("%lld\n", ans);
    }
    return 0;
}
